//
//  XHomeViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XHomeViewController.h"
#import "XDynamicViewController.h"
#import "XRecordViewController.h"
#import "XBookkeepingViewController.h"
#import "XRcordEditViewController.h"

@interface XHomeViewController (){
    NSArray* _pageInfos;
}



@end

@implementation XHomeViewController

- (instancetype)init{
    if(self = [super init]){
        
        [self initPageInfo];
        
        self.titleColorSelected = COLOR_WHITE;
        self.titleColorNormal = COLOR_WHITE;
        self.titleFontSelected = FONTMID(17);
        self.titleFontNormal = FONT_FIT(14);
        self.menuViewStyle = WMMenuViewStyleDefault;
        self.menuItemWidth = PX(70);
        self.menuViewContentMargin = 16;
        self.menuViewLayoutMode = WMMenuViewLayoutModeLeft;
        self.preloadPolicy = WMPageControllerPreloadPolicyNeighbour;
        self.showOnNavigationBar = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"记录";
    self.menuView.backgroundColor = COLOR_THEME_A;
    self.scrollView.showsHorizontalScrollIndicator = NO;
}

- (void)initPageInfo{
    NSMutableArray* pages = [NSMutableArray array];
    [pages addObject:@{@"index":@(0),@"title":@"动态",@"controller":@"XDynamicViewController"}];
    [pages addObject:@{@"index":@(1),@"title":@"记事",@"controller":@"XRecordViewController"}];
    [pages addObject:@{@"index":@(2),@"title":@"记账",@"controller":@"XBookkeepingViewController"}];
    _pageInfos = pages;
}



#pragma mark -- WMPageControllerDataSource

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return _pageInfos.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    
    NSDictionary* pageInfo = _pageInfos[index];
    return pageInfo[@"title"];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    
    NSDictionary *pageInfo = _pageInfos[index];
    NSString *controllerName = pageInfo[@"controller"];

    UIViewController* subontroller = [NSClassFromString(controllerName) new];
    return subontroller;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    return CGRectMake(0, 0, self.view.frame.size.width, PX(44));
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, SCREEN_HEIGHT - originY - IPHONE_TAB_BAR_HEIGHT - CGRectGetHeight(self.tabBarController.tabBar.frame));
}
@end
